package com.hph.user.repository;

import com.hph.user.domain.User;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author {USER}
 * @date {date}
 */
@Repository
public class UserRepository {

    private ConcurrentHashMap<Long,User> userCollection = new ConcurrentHashMap<>();

    private final static AtomicLong ID_GENERATOR = new AtomicLong(0);

    public Boolean save(User user) {

        Long id = ID_GENERATOR.incrementAndGet();

        user.setId(id);

        return userCollection.putIfAbsent(id, user) == null;
    }

    public Collection<User> listAll() {

        return userCollection.values();
    }
}
